package com.fire.control.system.support.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class XdaoDate extends Date{
	
	private static final long serialVersionUID = 1L;
	
	/**   
	 * @Fields format : 格式化格式  
	 */ 
	private String format = "yyyy-MM-dd HH:mm:ss";
	
	/**   
	 * @Fields dateFormat : 格式化
	 */ 
	private SimpleDateFormat dateFormat = new  SimpleDateFormat(format);
	
	public XdaoDate(){
		super();
	}
	
	public XdaoDate(long date){
		super(date);
	}
	/***
	 * 
	 * @Title: getFormatDate   
	 * @Description: 获取格式化时间
	 * @param:  @return      
	 * @return: String      
	 * @throws
	 */
	public String getFormatDate(){
		return this.toString();
	}
	/***
	 * 
	 * @Title: setFormat   
	 * @Description: 设置格式化格式 
	 * @param:  @param format      
	 * @return: void      
	 * @throws
	 */
	public void setFormat(String format){
		this.format = format;
		this.dateFormat = new  SimpleDateFormat(format); 
	}
	
	public String getFormat(){
		return this.format;
	}
	
	@Override
	public String toString(){
		return dateFormat.format(this);
	}
	
	public static String dateTimeFormatter(LocalDateTime localDateTime){
		return DateTimeFormatter.ofPattern("ss mm HH dd MM ? yyyy").format(localDateTime);
	}
	
	public static String dateToCron(String dateStr){
		try {
			return dateToCron(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return dateToCron(LocalDateTime.now());
	}
	
	public static String dateToCron(Date date){
		Instant instant = date.toInstant();
	    ZoneId zone = ZoneId.systemDefault();
	    LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
	    return dateTimeFormatter(localDateTime);
	}
	
	public static String dateToCron(LocalDateTime localDateTime){
		return dateTimeFormatter(localDateTime);
	}
	
	public static Date LocalDateTimeToUdate(LocalDateTime localDateTime) {
	    ZoneId zone = ZoneId.systemDefault();
	    Instant instant = localDateTime.atZone(zone).toInstant();
	    Date date = Date.from(instant);
	    return date;
	}
	
	

}
